package com.longge;

public class ConvertBST {

    int count = 0;

    public TreeNode convertBST(TreeNode root) {


        return rebuildTree(root);
    }

    private TreeNode rebuildTree(TreeNode root) {
        if (root == null) return null;
        root.right = rebuildTree(root.right);
        count += root.val;
        root.val = count;
        root.left = rebuildTree(root.left);
        return root;
    }


}
